{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from gearbox_model import RxGearboxModel, TxGearboxModel\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tx Gearbox"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "n_frames = 200\n",
    "header = [[f'{y:02d}-H0{x}' for x in range(2)] for y in range(n_frames)]\n",
    "data = [[f'{y:02d}-D{x:02d}' for x in range(64)] for y in range(n_frames)]\n",
    "\n",
    "\n",
    "txg = TxGearboxModel('str')\n",
    "txg_odata = []\n",
    "\n",
    "print(txg.get_state_header())\n",
    "\n",
    "for h, d in zip(header, data):\n",
    "    for _ in range(2):\n",
    "        idata = d[32:] if txg.get_frame_word() else d[:32]\n",
    "        ret = txg.next(h, idata)\n",
    "        txg_odata.append(ret['data'])\n",
    "        print(txg.get_state())\n",
    "\n",
    "        if (ret['pause']):\n",
    "            ret = txg.next(h, idata)\n",
    "            txg_odata.append(ret['data'])\n",
    "            print(txg.get_state())"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Rx Gearbox - no slip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rxg = RxGearboxModel('str')\n",
    "\n",
    "rxg_oheader =[]\n",
    "rxg_odata = []\n",
    "\n",
    "for id in txg_odata:\n",
    "    ret = rxg.next(id)\n",
    "    print(rxg.get_state())\n",
    "\n",
    "    if ret['header_valid']:\n",
    "        rxg_oheader.append(ret['header'])\n",
    "\n",
    "    if ret['data_valid']:\n",
    "        rxg_odata.append(ret['data'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# check the data\n",
    "\n",
    "# stitch the data back together in to frames\n",
    "\n",
    "\n",
    "\n",
    "oframes = [list(np.concatenate([x,y])) for x,y in zip(rxg_odata[::2], rxg_odata[1::2])]\n",
    "\n",
    "\n",
    "head_match = [ih == oh for ih, oh in zip(header, rxg_oheader)]\n",
    "data_match = [id == od for id, od in zip(data, oframes)]\n",
    "\n",
    "plt.plot(head_match)\n",
    "plt.plot(data_match)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Rx Gearbox - half slip needed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# try with 1 slip out\n",
    "rx_idata = txg_odata.copy()\n",
    "rx_idata = [item for sublist in rx_idata for item in sublist]\n",
    "for _ in range(1): rx_idata.insert(0, 'XXX')\n",
    "\n",
    "def chunks(lst, n):\n",
    "    \"\"\"Yield successive n-sized chunks from lst.\"\"\"\n",
    "    for i in range(0, len(lst), n):\n",
    "        yield lst[i:i + n]\n",
    "\n",
    "rx_idata = list(chunks(rx_idata, 32))[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rxg = RxGearboxModel('str')\n",
    "slip_next = False\n",
    "\n",
    "rxg_oheader = []\n",
    "rxg_odata = []\n",
    "aligned_idx = 0\n",
    "aligned = False\n",
    "\n",
    "for i, id in enumerate(rx_idata):\n",
    "    ret = rxg.next(id, slip_next)\n",
    "    print(rxg.get_state())\n",
    "\n",
    "    if not aligned:\n",
    "        aligned = ('H00' in ret['header'][0] and 'H01' in ret['header'][1]) and ret['header_valid'] \n",
    "\n",
    "    slip_next = ret['header_valid'] and not aligned and not slip_next \n",
    "\n",
    "    if ret['header_valid'] and aligned:\n",
    "        rxg_oheader.append(ret['header'])\n",
    "        rxg_odata.append([]) # append data placeholder\n",
    "\n",
    "    if ret['data_valid'] and aligned:\n",
    "        rxg_odata[-1] = list(np.concatenate([rxg_odata[-1], ret['data']]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "start_offset = 20 # if half slip, miss frame when first aligning\n",
    "\n",
    "# align the rx / test data\n",
    "\n",
    "start_idx = 0\n",
    "for i in range(len(header)):\n",
    "    if header[i] == rxg_oheader[start_offset]:\n",
    "        start_idx = i\n",
    "        print(f'Aligned at {i}')\n",
    "\n",
    "head_match = [ih == oh for ih, oh in zip(header[start_idx:], rxg_oheader[start_offset:])]\n",
    "data_match = [id == od for id, od in zip(data[start_idx:], rxg_odata[start_offset:])]\n",
    "plt.plot(head_match)\n",
    "plt.plot(data_match)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
